package com.zx.framework.core.util.gson;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
/**
 * json与对象操作的工具类
 * @author stylewolf
 *
 */
public class GsonUtil {
	
	private static final Logger LOGGER = Logger.getLogger(GsonUtil.class);
	/**
	 * 将map转换为javabean
	 * @param <T>
	 * @param map
	 * @param clazz
	 * @return
	 */
	public static <T> T map2Bean(Map<String, Object> map,Class<T> clazz){
		Gson gson = getGsonBuidler().create();
		String json = gson.toJson(map);
		return gson.fromJson(json, clazz);
	}
	
	/**
	 * 将对象转换为map
	 * @param <T>
	 * @param map
	 * @param clazz
	 * @return
	 */
	public static Map<String, Object> object2Map(Object bean){
		Gson gson = getGsonBuidler().create();
		String json = gson.toJson(bean);
		Type type = new TypeToken<Map<String, Object>>() {}.getType();
		return gson.fromJson(json, type);
	}
	/**
	 * 将对象转换为json串
	 * @param bean
	 * @return
	 */
	public static String object2Json(Object bean){
		Gson gson = getGsonBuidler().create();
		return gson.toJson(bean);
	}
	/**
	 * 将json字符串转换为map
	 * @param json
	 * @return
	 */
	public static Map<String, Object> json2Map(String json){
		Gson gson = getGsonBuidler().create();
		Type type = new TypeToken<Map<String, Object>>() {}.getType();
		return gson.fromJson(json, type);
	}
	/**
	 * 将json转换为list,无法识别传入泛型，不建议使用
	 * @param json
	 * @param clazz
	 * @return
	 */
	@Deprecated
	public static <T> List<T> json2List(String json,Class<T> clazz){
		Gson gson = getGsonBuidler().create();
		Type type = new TypeToken<List<T>>() {}.getType();
		return gson.fromJson(json, type);
	}
	
	public static <T> List<T> json2List(String json,Type type){
		Gson gson = getGsonBuidler().create();
		return gson.fromJson(json, type);
	}
	/**
	 * json字符串转换为对象
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> T json2Bean(String json,Class<T> clazz){
		Gson gson = getGsonBuidler().create();
		return gson.fromJson(json, clazz);
	}
	/**
	 * 获取日期设置器
	 * @param dateFormats
	 * @return
	 */
	public static final String[] getDateFormats(String[] dateFormats){
		if(ArrayUtils.isEmpty(dateFormats)){
			return GsonDateFormator.DATE_FORMATS;
		}
		else{
			return dateFormats;
		}
	}
	/**
	 * 获取多日期格式的builder
	 * @param dateFormats
	 * @return
	 */
	public static final GsonBuilder getGsonBuidler(String[] dateFormats){
		GsonBuilder builder =  new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new GsonDateFormator());
		builder.registerTypeAdapter(java.sql.Date.class, new GsonDateFormator());
		return builder;
	}
	/**
	 * 获取默认格式的builder
	 * @return
	 */
	public static final GsonBuilder getGsonBuidler(){
		GsonBuilder builder =  new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new GsonDateFormator());
		builder.registerTypeAdapter(java.sql.Date.class, new GsonDateFormator());
		return builder;
	}
}
